package com.xuelang.pipeline.app.project.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xuelang.pipeline.app.project.ProjectService;
import com.xuelang.pipeline.app.project.converter.ProjectToDTOConverter;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.project.dto.ProjectDTO;
import com.xuelang.pipeline.client.project.dto.ProjectQuery;
import com.xuelang.pipeline.domain.project.Project;
import com.xuelang.pipeline.domain.project.service.ProjectDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>项目服务实现类</p>
 *
 * @author : yixin.lys
 * @date : 2024-09-03 10:52
 **/
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectDomainService projectDomainService;

    @Override
    public PageRecord<ProjectDTO> pageProjects(PageParam<ProjectQuery> pageParam) {
        Long currentPage = Objects.nonNull(pageParam.getCurrentPage()) ? pageParam.getCurrentPage() : 1L;
        Long pageSize = Objects.nonNull(pageParam.getPageSize()) ? pageParam.getPageSize() : 10L;
        ProjectQuery condition = pageParam.getCondition();
        if(Objects.isNull(condition)){
            return new PageRecord<>(currentPage, pageSize, 0L, 0L, null);
        }
        PageRecord<Project> pageResult = projectDomainService.pageProjects(currentPage, pageSize, condition);
        return new PageRecord<>(currentPage,
                pageSize,
                pageResult.getTotalCount(),
                pageResult.getTotalPages(),
                pageResult.getRecords().stream().map(ProjectToDTOConverter.INSTANCE::toDTO).collect(Collectors.toList()));
    }

    @Override
    public Integer createProject(ProjectDTO projectDTO) {
        return projectDomainService.createProject(ProjectToDTOConverter.INSTANCE.fromDTO(projectDTO));
    }

    @Override
    public boolean deleteProjectById(Integer id) {
        return projectDomainService.deleteProjectById(id);
    }

    @Override
    public boolean updateProject(ProjectDTO projectDTO) {
        return projectDomainService.updateProject(ProjectToDTOConverter.INSTANCE.fromDTO(projectDTO));
    }

    @Override
    public ProjectDTO findProjectById(Integer id) {
        return ProjectToDTOConverter.INSTANCE.toDTO(projectDomainService.findProjectById(id));
    }

    @Override
    public ProjectDTO findProjectByCode(String code) {
        return ProjectToDTOConverter.INSTANCE.toDTO(projectDomainService.findProjectByCode(code));
    }

    @Override
    public List<ProjectDTO> findAllProjects() {
        List<Project> allProjects = projectDomainService.findAllProjects();
        if (CollectionUtils.isNotEmpty(allProjects)) {
            return allProjects.stream().map(ProjectToDTOConverter.INSTANCE::toDTO).collect(Collectors.toList());
        }
        return null;
    }
}
